package cn.intotw.rdcj.admin.shiro;

import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheException;
import org.apache.shiro.cache.CacheManager;
import org.springframework.cache.support.SimpleValueWrapper;

import java.util.Collection;
import java.util.Set;

/**
 * Created by Chenxiang
 *
 * @generator: IntelliJ IDEA
 * @description: ShiroCache
 * @project: rdcj
 * @package: cn.intotw.rdcj.admin.shiro
 * @date: 2021年06月04日 09时28分
 */
public class ShiroCacheManager implements CacheManager {
    private org.springframework.cache.CacheManager cacheManager;

    public ShiroCacheManager(org.springframework.cache.CacheManager cacheManager) {
        this.cacheManager = cacheManager;
    }

    @Override
    public <K, V> Cache<K, V> getCache(String s) throws CacheException {
        org.springframework.cache.Cache springCache = this.cacheManager.getCache(s);
        return new ShiroCacheManager.SpringCacheWrapper(springCache);
    }
    static class SpringCacheWrapper implements Cache {
        private org.springframework.cache.Cache springCache;

        SpringCacheWrapper(org.springframework.cache.Cache springCache) {
            this.springCache = springCache;
        }
        @Override
        public Object get(Object key) throws CacheException {
            Object value = this.springCache.get(key);
            return value instanceof SimpleValueWrapper ? ((SimpleValueWrapper)value).get() : value;
        }
        @Override
        public Object put(Object key, Object value) throws CacheException {
            this.springCache.put(key, value);
            return value;
        }
        @Override
        public Object remove(Object key) throws CacheException {
            this.springCache.evict(key);
            return null;
        }
        @Override
        public void clear() throws CacheException {
            this.springCache.clear();
        }
        @Override
        public int size() {
            throw new UnsupportedOperationException("invoke spring cache abstract size method not supported");
        }
        @Override
        public Set keys() {
            throw new UnsupportedOperationException("invoke spring cache abstract keys method not supported");
        }
        @Override
        public Collection values() {
            throw new UnsupportedOperationException("invoke spring cache abstract values method not supported");
        }
    }
}
